package lee.vioson.ebook.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.ui.Constants
import lee.vioson.ebook.ui.navigate
import lee.vioson.ebook.viewmodels.FeedbackDetailViewModel
import lee.vioson.remotedata.bean.FeedbackDetail
import lee.vioson.remotedata.bean.boolean

/**
 *Author:viosonlee
 *Date:2023/6/25
 *DESCRIPTION:
 */
@Composable
fun FeedbackDetail(
    msgId: String,
    navHostController: NavHostController,
    appContainer: AppContainer,
    feedbackDetailViewModel: FeedbackDetailViewModel = viewModel(factory = viewModelFactory {
        initializer { FeedbackDetailViewModel(appContainer, msgId) }
    })
) {
    val details by feedbackDetailViewModel.detail.collectAsState()

    Scaffold(
        topBar = {
            TopBar(
                title = stringResource(id = R.string.Feedback),
                navHostController = navHostController,
                action = {
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .clickable {
                                navHostController.navigate(
                                    AppDestinations.FEEDBACK,
                                    mapOf(Constants.msgId to msgId)
                                )
                            }, contentAlignment = Alignment.Center
                    ) {
                        Image(
                            painter = painterResource(id = R.mipmap.ic_feedback),
                            contentDescription = "do feedback",
                            Modifier.size(24.dp)
                        )
                    }
                })
        }
    ) { paddingValues ->
        LazyColumn(modifier = Modifier.padding(paddingValues), content = {
            items(details) { item: FeedbackDetail ->
                FeedbackDetailItem(feedbackDetail = item)
            }
        })
    }
}

@Composable
fun FeedbackDetailItem(feedbackDetail: FeedbackDetail) {
    Column(
        Modifier
            .fillMaxWidth()
            .padding(15.dp)
    ) {
        val textStyle = TextStyle(fontSize = 14.sp, color = AppTheme.appColors.grayTextColor)
        Row(Modifier.fillMaxWidth()) {
            Text(
                text = feedbackDetail.messageUserName,
                style = textStyle
            )
            Text(text = feedbackDetail.messageDateStr, style = textStyle)
        }
        if (!feedbackDetail.isReply.boolean) {
            Spacer(modifier = Modifier.height(10.dp))
            Text(text = stringResource(id = R.string.type_, feedbackDetail.type), style = textStyle)
        }
        Spacer(modifier = Modifier.height(10.dp))
        //photos
        feedbackDetail.photoList?.let { photoList ->
            LazyRow(content = {
                items(photoList) {
                    AsyncImage(
                        model = it, contentDescription = "feedback pic",
                        modifier = Modifier
                            .padding(8.dp)
                            .size(60.dp)
                    )
                }
            })
        }
        Spacer(modifier = Modifier.height(10.dp))
        Text(
            text = feedbackDetail.messageContent,
            style = textStyle.copy(textAlign = TextAlign.Start),
        )
    }
}